import _defineProperty from 'babel-runtime/helpers/defineProperty'
import _objectWithoutProperties from 'babel-runtime/helpers/objectWithoutProperties'
import _extends from 'babel-runtime/helpers/extends'
import { switchPropTypes } from './PropTypes'
import BaseMixin from 'ant-design-vue/es/_util/BaseMixin'
import {
  hasProp,
  getOptionProps,
  getComponentFromProp,
  getListeners
} from 'ant-design-vue/es/_util/props-util'

// function noop () {
// }
export default {
  name: 'ZVcSwitch',
  mixins: [BaseMixin],
  model: {
    prop: 'checked',
    event: 'change'
  },
  props: _extends({}, switchPropTypes, {
    prefixCls: switchPropTypes.prefixCls.def('rc-switch')
    // onChange: switchPropTypes.onChange.def(noop),
    // onClick: switchPropTypes.onClick.def(noop),
  }),
  data () {
    let checked = '0'
    if (hasProp(this, 'checked')) {
      checked = this.checked
    } else {
      checked = this.defaultChecked
    }
    return {
      stateChecked: checked
    }
  },

  watch: {
    checked (val) {
      this.stateChecked = val
    }
  },
  mounted () {
    const _this = this

    this.$nextTick(function () {
      const autoFocus = _this.autoFocus
      const disabled = _this.disabled
      if (autoFocus && !disabled) {
        _this.focus()
      }
    })
  },

  methods: {
    setChecked: function setChecked (checked, e) {
      if (this.disabled) {
        return
      }
      if (!hasProp(this, 'checked')) {
        this.stateChecked = checked
      }
      this.$emit('change', checked, e)
    },
    handleClick: function handleClick (e) {
      const checked = this.stateChecked == '1' ? '0' : '1'
      this.setChecked(checked, e)
      this.$emit('click', checked, e)
    },
    handleKeyDown: function handleKeyDown (e) {
      if (e.keyCode === 37) {
        // Left
        this.setChecked('0', e)
      } else if (e.keyCode === 39) {
        // Right
        this.setChecked('1', e)
      }
    },
    handleMouseUp: function handleMouseUp (e) {
      if (this.$refs.refSwitchNode) {
        this.$refs.refSwitchNode.blur()
      }
      this.$emit('mouseup', e)
    },
    focus: function focus () {
      this.$refs.refSwitchNode.focus()
    },
    blur: function blur () {
      this.$refs.refSwitchNode.blur()
    }
  },
  render: function render () {
    let _switchClassName

    const h = arguments[0]

    const _getOptionProps = getOptionProps(this)
    const prefixCls = _getOptionProps.prefixCls
    const disabled = _getOptionProps.disabled
    const loadingIcon = _getOptionProps.loadingIcon
    const tabIndex = _getOptionProps.tabIndex
    const restProps = _objectWithoutProperties(_getOptionProps, [
      'prefixCls',
      'disabled',
      'loadingIcon',
      'tabIndex'
    ])

    const checked = this.stateChecked == '1'
    const switchClassName =
      ((_switchClassName = {}),
        _defineProperty(_switchClassName, prefixCls, true),
        _defineProperty(_switchClassName, prefixCls + '-checked', checked),
        _defineProperty(_switchClassName, prefixCls + '-disabled', disabled),
        _switchClassName)
    const spanProps = {
      props: _extends({}, restProps),
      on: _extends({}, getListeners(this), {
        keydown: this.handleKeyDown,
        click: this.handleClick,
        mouseup: this.handleMouseUp
      }),
      attrs: {
        type: 'button',
        role: 'switch',
        'aria-checked': checked,
        disabled: disabled,
        tabIndex: tabIndex
      },
      class: switchClassName,
      ref: 'refSwitchNode'
    }
    return h('button', spanProps, [
      loadingIcon,
      h('span', { class: prefixCls + '-inner' }, [
        checked
          ? getComponentFromProp(this, 'checkedChildren')
          : getComponentFromProp(this, 'unCheckedChildren')
      ])
    ])
  }
}
